#include <iostream>

#include "CSignal.h"

using namespace std;

class CSIGRTMINHandler : public CSignalHandler {
public :
	CSIGRTMINHandler() {};
	~CSIGRTMINHandler() {};

	virtual int handle(int signum, siginfo_t *si, void *sv) {
		// for RTS
		if(si->si_code==SI_QUEUE) {
			assert(signum==SIGRTMIN);

			cerr << "user RTS signal = " << si->si_pid << endl;
			cerr << "signal number = " << si->si_signo << endl;
			cerr << "user data is " << si->si_value.sival_int << endl;

			// in order to test signal queueing
			getchar();
		}
		// for non-RTS
		else {
			cerr << "signal number = " << signum << endl;
		}
	};
};

int do_work()
{
	while(1) {
		sleep(1);
		cerr << "realtime signal testing" << endl;
	}
}

int main(int argc, char **argv)
{
	if(argc<2) {
		cout << "Usage : " << argv[0] << " [<pid> <num>]" << endl;
		cout << "\t\ton client : " << argv[0] << " " << getpid() << " 1234" << endl;

		CSIGRTMINHandler sigrt_handler;
		CSignal::instance()->register_handler(SIGRTMIN, &sigrt_handler);

		do_work();

		cerr << "never reached" << endl;
	}
	else {
		union sigval sv;

		sv.sival_int = atoi(argv[2]);
		CSignal::instance()->trigger(atoi(argv[1]), SIGRTMIN, sv);
	}
}
